package br.com.fogas.medicao.activity.helper;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.business.CriarPlanoVisitaBusiness;
import br.com.fogas.medicao.business.PlanoHomeBusiness;
import br.com.fogas.utils.dialog.ProgressDialogBuilder;
import br.com.fogas.utils.task.ServiceTask;
import br.com.fogas.utils.task.ServiceTaskBusiness;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class PlanoHomeHelper {
	
	private Fragment fragment;
	
	private Handler handler;
	
	private FogasDatabase database;

	@InjectView(R.id.contentHome) RelativeLayout contentHome;

	@InjectView(R.id.btnHomeCriarPlano) Button btnHomeCriarPlano;
	
	public PlanoHomeHelper(Fragment fragmentParent) {
		ButterKnife.inject(this, fragmentParent.getView());
		handler = new Handler();
		fragment = fragmentParent;
		database = new FogasDatabase(fragment.getActivity());
		contentHome.setVisibility(View.GONE);
		btnHomeCriarPlano.setVisibility(View.GONE);
		processar();
	}
	
	public void onDestroy() {
		database.fecharConexao();
		ButterKnife.reset(this);
	}
	
	public void mostrarViews() {
		handler.post(new Runnable() {
			public void run() {
				contentHome.setVisibility(View.VISIBLE);
				btnHomeCriarPlano.setVisibility(View.VISIBLE);
			}
		});
	}
	
	
	public void processar() {
		ServiceTaskBusiness business = new PlanoHomeBusiness(this);
		
		ProgressDialogBuilder dialog = new ProgressDialogBuilder(fragment.getActivity())
		.setMessage(fragment.getString(br.com.fogas.utils.R.string.dialog_processando));
		
		ServiceTask task = new ServiceTask(dialog, business);
		task.execute();
	}
	
	@OnClick(R.id.btnHomeCriarPlano)
	public void criarPlano() {
		ServiceTaskBusiness business = new CriarPlanoVisitaBusiness(
				fragment.getActivity().getSupportFragmentManager(), 
				database);
		
		ProgressDialogBuilder dialog = new ProgressDialogBuilder(fragment.getActivity())
		.setMessage(fragment.getString(br.com.fogas.utils.R.string.dialog_processando));
		
		ServiceTask task = new ServiceTask(dialog, business);
		task.execute();
	}
	
	public Fragment getFragment() {
		return fragment;
	}
	
	public FogasDatabase getDatabase() {
		return database;
	}
	
	public RelativeLayout getContentHome() {
		return contentHome;
	}
	
	public Button getBtnHomeCriarPlano() {
		return btnHomeCriarPlano;
	}
}